/**
 * Created by Salted fish
 * Description:
 * User: 24539
 * Date：2021-08-06
 * Time: 21:16
 */
class Node {
    public int val;
    public Node next;//null

    public Node(int val) {
        this.val = val;
    }
}
//链表
public class MyLinkedList {
    public Node head;//标识单链表的头节点
    /**
     * 穷举的方式 创建链表  当然很low。此处只是为了好理解
     */
   /* public void createList() {
        Node node1 = new Node(12);
        Node node2 = new Node(3);
        Node node3 = new Node(5);
        Node node4 = new Node(2);
       node1.next = node2;
       node2.next = node3;
       node3.next = node4;
       this.head = node1;
    }*/

    /**
     * 打印单链表
     */
    public void display() {
        Node cur = this.head;
        while (cur != null) {
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
        System.out.println();
    }

    //得到单链表的长度
    public int size() {
        Node cur = head;
        int count = 0;
        while (cur != null) {
            count++;
            cur = cur.next;
        }
        return count;
    }

    //查找是否包含关键字key是否在单链表当中
    public boolean contains(int key) {
        Node cur = this.head;
        while (cur != null) {
            if (cur.val == key) {
                return true;
            }
            cur = cur.next;
        }
        return false;
    }


    //头插法
    public void addFirst(int data) {
        Node node = new Node(data);
        node.next = head;
        head = node;
    }

    //尾插法
    public void addLast(int data) {
        Node node = new Node(data);
        if (head == null) {
            head = node;
        } else {
            Node cur = this.head;
            while (cur.next != null) {
                cur = cur.next;//找链表最后一个
            }//将最后一个next指向node的位置完成尾插法
            cur.next = node;
        }
    }

    public Node search(int index) {
        Node cur = this.head;
        int count = 0;//用count来得出cur走的几个位置
        while (count != index - 1) {
            cur = cur.next;
            count++;
        }
        return cur;
    }

    //任意位置插入,第一个数据节点为0号下标
    public void addIndex(int index, int data) {
        if (index < 0 || index > size()) {//判断输入的值是否不超出链表
            System.out.println("输入的位置非法");
            return;
        }
        if (index == 0) {
            addFirst(data);
            return;
        }
        if (index == size()) {
            addLast(data);
            return;
        }
        Node cur = this.head;
        Node node = new Node(data);
        node.next = cur.next;//先将被插入值的next值赋值给node.next
        cur.next = node;//在将node的值赋值给前一位的next
    }

    public Node seach(int key) {
        Node cur = this.head;
        while (cur.next != null) {
            if (cur.next.val == key) {
                return cur;
            }
            cur = cur.next;
        }
        return null;
    }

    //删除第一次出现关键字为key的节点
    public void remove(int key) {
        if (this.head == null) return;//判断是否为空链表
        if (this.head.val == key) {
            this.head = this.head.next;
            return;//如果删除的值是第一个
        }
        Node cur = search(key);
        if (cur == null) {
            System.out.println("没有要删除的值！");
            return;
        }
        Node del = cur.next;
        cur.next = del.next;
    }

    //删除所有值为key的节点
    public void removeAllKey(int key) {
        if (this.head == null) {
            return;
        }
        Node prev = this.head;
        Node cur = this.head.next;
        while (cur != null) {
            if (cur.val == key) {
                prev.next = cur.next;
                cur = cur.next;
            } else {
                prev = cur;
                cur = cur.next;
            }
        }
        if (this.head.val == key) {
            this.head = this.head.next;
        }
    }

    public void clear(){
        Node cur = this.head;
        while(cur!=null){
             cur=this.head.next;
            this.head.next=null;
            this.head=cur;
        }
    }

}


